package me.linczh.bank.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import me.linczh.bank.bean.AccountStatisticBean;
import me.linczh.bank.dao.AccountStatisticDao;
import me.linczh.framework.query.Query;

public class DBAccountStatisticDao extends BaseDBDaoImpl implements AccountStatisticDao {

	@Override
	public AccountStatisticBean create(AccountStatisticBean _statistic) {
		// TODO Auto-generated method stub
		try {
			Statement s = super.conn.createStatement();
			SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time;
			if (_statistic.getOverdrawtime() == null) time = "1990-08-18 00:00:00";
			else time = fmt.format(_statistic.getOverdrawtime());
			s.execute("insert into statistic_tbl values (" +
					"'" + _statistic.getAccount() + "'," +
					"'" + time + "'," +
					+ _statistic.getBalance() + "," +
					+ _statistic.getNumday() + "," +
					+ _statistic.getChargemonth() + ")");
			return _statistic;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean update(AccountStatisticBean _statistic) {
		// TODO Auto-generated method stub
		try {
			Statement s = super.conn.createStatement();
			SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time;
			if (_statistic.getOverdrawtime() == null) time = "1990-08-18 00:00:00";
			else time = fmt.format(_statistic.getOverdrawtime());
			int succ = s.executeUpdate("update statistic_tbl set " +
					"overdrawtime='" + time + "'," +
					"balance=" + _statistic.getBalance() + "," +
					"numday=" + _statistic.getNumday() + "," +
					"chargemonth=" + _statistic.getChargemonth() + " where " +
					"account='" + _statistic.getAccount() + "'");
			if (succ > 0) return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
		return false;
	}

	@Override
	public AccountStatisticBean findByAccount(String account) {
		// TODO Auto-generated method stub
		try {
			Statement s = super.conn.createStatement();
			SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			ResultSet result = s.executeQuery("select * from statistic_tbl where " +
					"account='" + account + "'");
			if (result.next()) {
				AccountStatisticBean bean = new AccountStatisticBean();
				bean.setAccount(result.getString(1));
				if (result.getString(2).equals("1990-08-18 00:00:00")) bean.setOverdrawtime(null);
				else bean.setOverdrawtime(fmt.parse(result.getString(2)));
				bean.setBalance(result.getDouble(3));
				bean.setNumday(result.getInt(4));
				bean.setChargemonth(result.getInt(5));
				return bean;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<AccountStatisticBean> query(Query<AccountStatisticBean> query) {
		// TODO Auto-generated method stub
		try {
			Statement s = super.conn.createStatement();
			List<AccountStatisticBean> list = new ArrayList<AccountStatisticBean>();
			SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			ResultSet result = s.executeQuery(query.statement());
			while (result.next()) {
				AccountStatisticBean bean = new AccountStatisticBean();
				bean.setAccount(result.getString(1));
				if (result.getString(2).equals("1990-08-18 00:00:00")) bean.setOverdrawtime(null);
				else bean.setOverdrawtime(fmt.parse(result.getString(2)));
				bean.setBalance(result.getDouble(3));
				bean.setNumday(result.getInt(4));
				bean.setChargemonth(result.getInt(5));
				list.add(bean);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ArrayList<AccountStatisticBean>();
	}

	@Override
	public boolean delete(AccountStatisticBean _statistic) {
		// TODO Auto-generated method stub
		try {
			Statement s = super.conn.createStatement();
			int succ = s.executeUpdate("delete from statistic_tbl where " +
					"account='" + _statistic.getAccount() + "'");
			if (succ > 0) return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

}
